""""
@author luzhenfang
@date 2022 05 07
@desc 成语接龙
"""
import sys
import os

try:
    import requests
except Exception as e:
    os.system("pip install requests")

import requests


class Response:
    def __init__(self, state):
        self.__dict__.update(state)


def get_response(key):
    url = f"https://www.cyjl123.com/api/web/search?keyword={key[-1:]}"
    headers = {}
    try:
        response = requests.request("GET", url, headers=headers).json()
        r = Response(response)
        if r.code != 0:
            raise Exception("请求出现错误")
        return r.result["oneData"][:1][0]
    except Exception as e:
        # print(e)
        sys.exit(1)


if __name__ == '__main__':
    # 要接龙的成语
    resp = get_response("如狼似虎")
    print(resp)
    end = (resp[-1:])
    for i in range(30):
        end = get_response(end)
        if end is None:
            break
        print(end)
        end = end[-1:]
